<?php
require_once 'vendor/autoload.php';

use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule;
$capsule->addConnection([
    'driver'    => 'mysql',
    'host'      => '129.226.194.102',
    'database'  => 'aimeos_lvgang_to',
    'username'  => 'aimeos_lvgang_to',
    'password'  => 'ZQaJw6X9nksn8kt7',
    'charset'   => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix'    => '',
]);

$capsule->setAsGlobal();
$capsule->bootEloquent();

try {
    $pdo = $capsule->getConnection()->getPdo();
    
    // Get the site ID for 'default' site
    $result = $pdo->query("SELECT id FROM mshop_locale_site WHERE code = 'default'");
    $siteRow = $result->fetch(PDO::FETCH_ASSOC);
    
    if ($siteRow) {
        $siteId = $siteRow['id'];
        echo "Found default site with ID: $siteId\n";
        
        // Update mshop_locale table with correct siteid format
        $updateSql = "UPDATE mshop_locale SET siteid = CONCAT(?, '.') WHERE siteid = 'default'";
        $stmt = $pdo->prepare($updateSql);
        $stmt->execute([$siteId]);
        echo "Updated mshop_locale siteid to: $siteId.\n";
        
        // Also ensure site_id is set correctly
        $updateSql2 = "UPDATE mshop_locale SET site_id = ? WHERE site_id IS NULL OR site_id != ?";
        $stmt2 = $pdo->prepare($updateSql2);
        $stmt2->execute([$siteId, $siteId]);
        echo "Updated mshop_locale site_id to: $siteId\n";
        
        // Check current locale data
        $result = $pdo->query("SELECT * FROM mshop_locale");
        echo "Current locale data:\n";
        while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
            echo "ID: {$row['id']}, siteid: {$row['siteid']}, site_id: {$row['site_id']}, langid: {$row['langid']}, currencyid: {$row['currencyid']}\n";
        }
        
    } else {
        echo "Default site not found!\n";
    }
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}